import { get as requestGet, post as requestPost } from '@/utils/requestUtil.js'
import { createCacher } from '../service/cache/cacheService'

//创建混存期
const cacher = createCacher('param', _get)

const { get, remove } = cacher

/**
 * 
 * @param {string} code 参数code 
 * @returns {Object} 参数
 */
async function _get(code) {
    const response = await requestGet(`/api/param/${code}`)
    if (response && response.data) {
        return response.data
    }
    else return null
}

/**
 * 参数项保存
 * @param {string} code 参数标识 
 * @param {Object} paramItem 参数项
 * @returns {Object} 接口返回结果
 */
async function saveParamItem(code, paramItem) {
    const response = await requestPost('/api/param/save/item', { body: JSON.stringify(paramItem) })
    if (response && response.code === 200) {
        //清除缓存
        remove(code)
        return response
    }
    return null
}

const paramUtil = {
    get,
    saveParamItem
}

export default paramUtil